package tech.yunjing.train.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tech.yunjing.train.entity.UserInfo;
import tech.yunjing.train.service.RegisterService;
import tech.yunjing.train.util.JsonResult;


/**
 * purpose：注册
 * Created by 吕祥 on 2017/6/9.
 */
@RestController
@RequestMapping(value = "/Register")
public class RegisterContorller extends BaseController{

    @Autowired
    RegisterService<UserInfo> registerService;

    /**
     * 校验 注册手机号是否存在
     * @param userAccount 手机账号
     * @return ture = 存在  false = 不存在(可用)
     */
    @RequestMapping(value = "/verifyUserAccount", method = RequestMethod.POST)
    public JsonResult<Boolean> verifyUserAccount(String userAccount) {
        return new JsonResult<Boolean>(registerService.verifyUserAccount(userAccount));
    }

    /**
     * 生成随机验证码并保存
     * @return 验证码
     */
    @RequestMapping(value = "/sendVerificationCode", method = RequestMethod.GET)
    public JsonResult<String> sendVerificationCode() {
        return new JsonResult<String>(registerService.sendVerificationCode());
    }

    /**
     * 校验用户注册信息并保存到 MongoDB
     * @param userAccount 用户账号
     * @param userPassword 用户密码
     * @param sex  性别
     * @param idNumber  身份证
     * @param site 住址
     * @param verificationCode 验证码
     * @return UserInfo 对象
     */
    @RequestMapping(value = "/verifyUserInfoAndSave", method = RequestMethod.POST)
    public JsonResult<UserInfo> verifyUserInfoAndSave(String userAccount, String userPassword, String sex,
                                                              String idNumber, String site, String verificationCode) {
        return new JsonResult(registerService.verifyUserInfoAndSave(userAccount,userPassword,sex,idNumber,site,verificationCode));
    }

    /**
     * 根据用户账号修改密码
     * @param userAccount 用户账号
     * @param userPassword 用户密码
     * @return 是否修改成功提示
     */
    @RequestMapping(value = "/updateUserAccount", method = RequestMethod.POST)
    public JsonResult<String> updateUserAccount(String userAccount, String userPassword, String updatePassword) {
        return new JsonResult<String>(registerService.updateOneUserAccount(userAccount, userPassword, updatePassword));
    }

    /**
     * 逻辑删除用户信息(修改用户状态)
     * @param userAccount 用户账号
     * @return 是否删除成功提示
     */
    @RequestMapping(value = "/removeUserStatus", method = RequestMethod.POST)
    public JsonResult<String> removeUserStatus (String userAccount, String userPassword) {
        return new JsonResult<String>(registerService.removeOneUserInfo(userAccount, userPassword));
    }

    /**
     * 根据用户账号查找 用户信息
     * @param userAccount 用户账号
     * @return 用户信息
     */
    @RequestMapping(value = "findByUserAccount", method = RequestMethod.POST)
    public JsonResult<UserInfo> findByUserAccount (String userAccount, String userPassword) {
        return new JsonResult<UserInfo>(registerService.findByUserAccount(userAccount, userPassword));
    }


}
